/**
 * Copyright (C),2011-2012,北京国电通网络技术有限公司.
 * FileName:	sqlite.cpp
 * Author:		LiuHaifeng
 * Version:		V0.1
 * Date:		2011-11-15
 * Description	This file realizes all kinds of operation of sqlite-database, include inquiry/insert/update/delete table, and so on.
 */

/**
 * History:
 * 		Author		date		Version		Description
 */

#include "sqlite3.h"
#include "sqlite.h"

extern struct StEquipList g_devList;
extern struct StDownTask g_downTask;
extern struct StDataTransmit g_dataTrans;
extern struct StOther g_other;

///used in this file
sqlite3* db;												///the pointer of database created or opened
sem_t DB_sem;
int DBTimes=0;
char cmdStr[SQLITECMDLEN];									///cmd string to be operate by "sqlite3_exec"\"sqlite3_get_table"
static int updateDatabase();
static int createDatabase();
static int openDatabase();
static void closeDatabase();
static int createTable(char*);

static int createDatabase()
{
	return openDatabase();
}
static int openDatabase()
{
	char* errmsgOpen=NULL;
	///open database
	if(db!=NULL)
		;
	else
	{
		if(SQLITE_OK!=sqlite3_open(DATABASENAME,&db))

		{
			ERROROUT("can't open database.");
			goto FAILED;
		}
		//chmod(DATABASENAME,0777);
		/// open foreign key support
		if(sqlite3_exec(db,"PRAGMA foreign_keys=ON;",NULL,NULL,&errmsgOpen))
		{
			ERROROUT("can't open foreign_keys.");
			goto FAILED;
		}
		sqlite3_free(errmsgOpen);
	}
	sem_wait(&DB_sem);
	DBTimes++;
	sem_post(&DB_sem);
	return 0;

	FAILED:
	sqlite3_free(errmsgOpen);
	return -1;
}
static void closeDatabase()
{
	sem_wait(&DB_sem);
	if(--DBTimes>0)
		;
	else
	{
		if(db!=NULL)
		{
			sqlite3_close(db);
			DBTimes=0;
			db=NULL;
		}
	}
	sem_post(&DB_sem);
}
static int createTable(char* name)
{
	char* errmsg=NULL;
	char ** l_result=NULL;									    	///result of tables in database
	int l_row=0;                                            	///the row of result
	int l_column=0;									        	///the column of result

	if(sqlite3_get_table(db,"SELECT name FROM sqlite_master WHERE type='table';",&l_result,&l_row,&l_column,&errmsg))
	{
		ERROROUT("read tables from database failed.");
		goto FAILED;
	}
	int l_i;
	for(l_i=0;l_i<l_row+1;l_i++)								///l_column=1;the first row is result[0]=name;result[1]=sqlite_sequence;and the other is the table names one in every row.
	{
		if(l_row==0)
			goto CREATE;										///first create table, must do this, because l_result is null and l_row or l_column is 0.
		if(!strcmp(l_result[l_i],name))
			break;
	}
	sqlite3_free_table(l_result);
	if(l_i<l_row+1)
	{
		PRINTOUT("%s","the table has exist.");
		goto OK;
	}
	sqlite3_free(errmsg);

	CREATE:														///label, take care!!!do not delete.

	if(!strcmp(name,"devlist"))
	{
		sprintf(cmdStr,"CREATE TABLE devlist(typeID INTEGER PRIMARY KEY,locationID INTEGER,iconID INTEGER,operID INTEGER,socketID NVARCHAR(16),roomName NVARCHAR(40),devName NVARCHAR(40),elecData DOUBLE);");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("create 'devlist' failed.");
			goto FAILED;
		}
		sqlite3_free(errmsg);
		sprintf(cmdStr,"INSERT INTO devlist VALUES(0,0,0,0,0,0,0,0);");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("insert '0' failed.");
			goto FAILED;
		}
	}
	else if (!strcmp(name,"power"))
	{
		sprintf(cmdStr,"CREATE TABLE power(typeID INTEGER,hourTime INTEGER,data DOUBLE,FOREIGN KEY(typeID) REFERENCES devlist(typeID) ON UPDATE CASCADE ON DELETE CASCADE);");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("create 'power' failed.");
			goto FAILED;
		}
	}
	else if (!strcmp(name,"actpower"))
	{
		sprintf(cmdStr,"CREATE TABLE actpower(typeID INTEGER,hourTime INTEGER,data DOUBLE,FOREIGN KEY(typeID) REFERENCES devlist(typeID) ON UPDATE CASCADE ON DELETE CASCADE);");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("create 'actpower' failed.");
			goto FAILED;
		}	
	}
	else if (!strcmp(name,"tmppower"))
	{
		sprintf(cmdStr,"CREATE TABLE tmppower(typeID INTEGER,hourTime INTEGER,data DOUBLE,FOREIGN KEY(typeID) REFERENCES devlist(typeID) ON UPDATE CASCADE ON DELETE CASCADE);");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("create 'tmppower' failed.");
			goto FAILED;
		}
	}
	else if(!strcmp(name,"time"))
	{
		sprintf(cmdStr,"CREATE TABLE time(typeID INTEGER,cmdType INTEGER,controlVal INTEGER,controlTime INTEGER,frequency INTEGER,remark NVARCHAR(40),FOREIGN KEY(typeID) REFERENCES devlist(typeID) ON UPDATE CASCADE ON DELETE CASCADE);");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("create 'time' failed.");
			goto FAILED;
		}
	}
	else if(!strcmp(name,"model"))
	{
		sprintf(cmdStr,"CREATE TABLE model(modeID INTEGER,name NVARCHAR(40));");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("create 'model' failed.");
			goto FAILED;
		}
	}
	else if(!strcmp(name,"modtime"))
	{
		sprintf(cmdStr,"CREATE TABLE modtime(typeID INTEGER,cmdType INTEGER,controlVal INTEGER,controlTime INTEGER,frequency INTEGER,remark NVARCHAR(40),modeID INTEGER,FOREIGN KEY(typeID) REFERENCES devlist(typeID) ON UPDATE CASCADE ON DELETE CASCADE);");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("create 'day' failed.");
			goto FAILED;
		}
	}
	else if(!strcmp(name,"global"))
	{
		sprintf(cmdStr,"CREATE TABLE global(ID INTEGER PRIMARY KEY AUTOINCREMENT,name NVARCHAR(20),value NVARCHAR(20));");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("create 'global' failed.");
			goto FAILED;
		}
		sqlite3_free(errmsg);
		///version
		sprintf(cmdStr,"INSERT INTO global VALUES(NULL,'version','%d');",PROVERSION);
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("insert 'global' failed.");
			goto FAILED;
		}
		sqlite3_free(errmsg);
		///username
		sprintf(cmdStr,"INSERT INTO global VALUES(NULL,'username','admin');");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("insert 'global' failed.");
			goto FAILED;
		}
		sqlite3_free(errmsg);
		///passwd
		sprintf(cmdStr,"INSERT INTO global VALUES(NULL,'passwd','admin');");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("insert 'global' failed.");
			goto FAILED;
		}
		sqlite3_free(errmsg);
		///serialNUM
		sprintf(cmdStr,"INSERT INTO global VALUES(NULL,'serialNUM','');");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("insert 'global' failed.");
			goto FAILED;
		}
		sqlite3_free(errmsg);
		///serverIP
		sprintf(cmdStr,"INSERT INTO global VALUES(NULL,'serverIP','');");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("insert 'global' failed.");
			goto FAILED;
		}
		sqlite3_free(errmsg);
		///port
		sprintf(cmdStr,"INSERT INTO global VALUES(NULL,'port','');");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("insert 'global' failed.");
			goto FAILED;
		}
		sqlite3_free(errmsg);
	}
	else if(!strcmp(name,"writelog"))
	{
		sprintf(cmdStr,"CREATE TABLE writelog(ID INTEGER PRIMARY KEY AUTOINCREMENT,cmd  NVARCHAR(20),time NVARCHAR(12));");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("create 'writelog' failed.");
			goto FAILED;
		}
	}
	else if(!strcmp(name,"readlog"))
	{
		sprintf(cmdStr,"CREATE TABLE readlog(ID INTEGER PRIMARY KEY AUTOINCREMENT,cmd  NVARCHAR(20),time NVARCHAR(12));");
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("create 'readlog' failed.");
			goto FAILED;
		}
	}

	OK:
	sqlite3_free(errmsg);
	return 0;

	FAILED:
	sqlite3_free(errmsg);
	closeDatabase();
	return -1;
}
/***sqliteInit() brief init sqlite, include open database, create table, and so on.
 * param null
 *
 * retval int,0 if successful or -1 failed
 ***/
int sqliteInit()
{
	db=NULL;
	///initialize DB_sem
	if(-1==sem_init(&(DB_sem),0,1))
	{
		ERROROUT("DB_sem init failed!");
		return -1;
	}
	if(-1==createDatabase())						///open database
		return -1;
	///see need update
	if (-1==updateDatabase())					/// update database
		return -1;
	return 0;						/// quit
}
/***getGlobalPara() brief get some global variables from sqlite, such as 'version'.
 * param name[in], the name of global param
 *
 * retval int,0 if successful or -1 failed
 ***/
int getGlobalPara(char* name)
{
	char* errmsg=NULL;
	char** l_result = NULL;
	int l_row = 0;
	int l_column = 0;
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	if(!strcmp(name,"version"))
		sprintf(cmdStr,"SELECT value FROM global WHERE name='version';");
	if(sqlite3_get_table(db,cmdStr,&l_result,&l_row,&l_column,&errmsg))
	{
		ERROROUT("query failed.");
		goto FAILED;
	}
	///close database
	sqlite3_free(errmsg);
	sqlite3_free_table(l_result);
	closeDatabase();
	return atoi(l_result[1]);

	FAILED:
	sqlite3_free(errmsg);
	sqlite3_free_table(l_result);
	closeDatabase();
	return -1;
}
/***initEquipFromSqlite() brief update main-program's g_devList while program boots
 * param null
 *
 * retval int,0 if successful or -1 failed
 ***/
int initEquipFromSqlite()
{
	char* errmsg=NULL;
	char** l_result=NULL;
	int l_row=0;
	int l_column=0;
	char l_tempStr[4];
	int l_pos=0;
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	///read devlist
	sprintf(cmdStr,"SELECT typeID,operID,socketID,elecData FROM devlist;");
	if(sqlite3_get_table(db,cmdStr,&l_result,&l_row,&l_column,&errmsg))
	{
		ERROROUT("query failed.");
		goto FAILED;
	}
	for(int i=1;i<(l_row+1);i++)
	{
		///initialize
		if(i>EQUIPMENT_NUM+1)					///???include typeID=0
		{
			ERROROUT("more than EQUIPMENT_NUM equipments!");
			goto FAILED;
		}
		if(0==atoi(l_result[i*l_column]))
			continue;
		l_pos=getElem(g_devList.st_empty);
		if(-1==l_pos)
		{
			ERROROUT("no enough struct in 'g_devList.st_empty'!");
			goto FAILED;
		}
		g_devList.st_equipArray[l_pos].elecQueryStatus=sta_no;
		g_devList.st_equipArray[l_pos].activeQueryStatus=sta_no;
		g_devList.st_equipArray[l_pos].firstElec=equ_no;
		g_devList.st_equipArray[l_pos].typeID=atoi(l_result[i*l_column]);
		g_devList.st_equipArray[l_pos].operID=atoi(l_result[i*l_column+1]);
		///make sure every device's 'socketID' have 16 char length, or 'segment fault'
		for(int j=0;j<8;j++)
		{
			strncpy(l_tempStr,l_result[i*l_column+2]+j*2,2);
			l_tempStr[2]='\0';
			g_devList.st_equipArray[l_pos].equipmentID[j]=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
		}
		g_devList.st_equipArray[l_pos].lastHourElecQuan=atof(l_result[i*l_column+3]);
		///insert into list
		insertElem(g_devList.st_equip,l_pos);
	}

	///close database
	sqlite3_free(errmsg);
	sqlite3_free_table(l_result);
	closeDatabase();
	return 0;

	FAILED:
	sqlite3_free_table(l_result);
	sqlite3_free(errmsg);
	closeDatabase();
	return -1;
}
/***devSyn() brief synchronous devlist--include 'set send-flag'
 *
 * retval int,0 if successful or -1 failed
 ***/
int devSyn()
{
	char* errmsg=NULL;
	char** l_result = NULL;
	int l_row = 0;
	int l_column = 0;
	char l_tempStr[12];
	int l_sendNum = 0;
	int l_pos;
	int l_temp;
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	sprintf(cmdStr,"SELECT typeID,locationID,iconID,roomName,devName FROM devlist;");
	if(sqlite3_get_table(db,cmdStr,&l_result,&l_row,&l_column,&errmsg))
	{
		ERROROUT("query failed.");
		goto FAILED;
	}
	for(int i=1;i<(l_row+1);i++)
	{
		l_pos=findByID(atoi(l_result[i*l_column]),find_typeID);
		if(-1==l_pos)
		{
			PRINTOUT("%s","no this device in devlist.");
			continue;					///memory and datebase disagreement, some error happened
		}
		sprintf(l_tempStr,"%06x",g_devList.st_equipArray[l_pos].typeID);
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[2];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[3];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[4];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[5];
		g_other.st_pCmdOther[l_sendNum++]=';';
		sprintf(l_tempStr,"%02x",atoi(l_result[i*l_column+1]));		///locationID
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
		g_other.st_pCmdOther[l_sendNum++]=';';
		sprintf(l_tempStr,"%02x",atoi(l_result[i*l_column+2]));		///icon
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
		g_other.st_pCmdOther[l_sendNum++]=';';
		///operID
		sprintf(l_tempStr,"%04x",g_devList.st_equipArray[l_pos].operID);
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[2];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[3];
		g_other.st_pCmdOther[l_sendNum++]=';';
		///socketID
		sprintf(l_tempStr,"%02x%02x%02x%02x",g_devList.st_equipArray[l_pos].equipmentID[0],g_devList.st_equipArray[l_pos].equipmentID[1],g_devList.st_equipArray[l_pos].equipmentID[2],g_devList.st_equipArray[l_pos].equipmentID[3]);
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[2];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[3];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[4];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[5];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[6];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[7];
		sprintf(l_tempStr,"%02x%02x%02x%02x",g_devList.st_equipArray[l_pos].equipmentID[4],g_devList.st_equipArray[l_pos].equipmentID[5],g_devList.st_equipArray[l_pos].equipmentID[6],g_devList.st_equipArray[l_pos].equipmentID[7]);
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[2];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[3];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[4];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[5];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[6];
		g_other.st_pCmdOther[l_sendNum++]=l_tempStr[7];
		g_other.st_pCmdOther[l_sendNum++]=';';

		l_temp=strlen(l_result[i*l_column+3]);						///roomName
		memcpy(&g_other.st_pCmdOther[l_sendNum],l_result[i*l_column+3],l_temp);
		l_sendNum+=l_temp;
		g_other.st_pCmdOther[l_sendNum++]=';';
		l_temp=strlen(l_result[i*l_column+4]);						///devName
		memcpy(&g_other.st_pCmdOther[l_sendNum],l_result[i*l_column+4],l_temp);
		l_sendNum+=l_temp;
		g_other.st_pCmdOther[l_sendNum++]=';';
		g_other.st_pCmdOther[l_sendNum++]='@';
	}
	g_other.st_pCmdOther[l_sendNum++]='#';
	g_other.st_pCmdOther[l_sendNum++]='\0';
	///return
	sqlite3_free(errmsg);
	sqlite3_free_table(l_result);
	closeDatabase();
	return 0;

	FAILED:
	sqlite3_free(errmsg);
	sqlite3_free_table(l_result);
	closeDatabase();
	return -1;
}
/***addDev() brief add device to devlist, and create hour-elec-table
 * param pos[in], the position of 'struct StControlCmd'
 *
 * retval int,0 if successful or -1 failed
 ***/
int addDev(int pos)
{
	char* errmsg=NULL;
	int l_typeID;
	int l_locationID;
	int l_iconID;
	int l_operID;
	int l_roomName;
	int l_devName;
	char l_tempStr[20];
	int l_temp;
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	///insert
	l_typeID=g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2];
	memcpy(l_tempStr,&g_other.st_pCmdOther[0],2);		///locationID
	l_tempStr[2]=0;
	l_locationID=strtol(l_tempStr,(char**)NULL,16);

	memcpy(l_tempStr,&g_other.st_pCmdOther[2],2);		///iconID
	l_tempStr[2]=0;
	l_iconID=strtol(l_tempStr,(char**)NULL,16);

	if(0x01==g_downTask.st_command[pos].param[1])		///locationID+iconID+socketID+roomName+devName
	{
		memcpy(l_tempStr,&g_other.st_pCmdOther[4],16);	///socketID
		l_tempStr[16]=0;
		l_operID=0;
		l_roomName=20;
	}
	else if(0x02==g_downTask.st_command[pos].param[1])	///locationID+iconID+operID+roomName+devName
	{
		memcpy(l_tempStr,&g_other.st_pCmdOther[4],4);	///operID
		l_tempStr[4]=0;
		l_operID=strtol(l_tempStr,(char**)NULL,16);
		l_roomName=8;
		l_tempStr[0]='0';								///socketID
		l_tempStr[1]='\0';
	}
	else if(0x03==g_downTask.st_command[pos].param[1])	///locationID+iconID+operID+socketID+roomName+devName
	{
		memcpy(l_tempStr,&g_other.st_pCmdOther[4],4);	///operID
		l_tempStr[4]=0;
		l_operID=strtol(l_tempStr,(char**)NULL,16);
		memcpy(l_tempStr,&g_other.st_pCmdOther[8],16);	///socketID
		l_tempStr[16]=0;
		l_roomName=24;
	}
	else
	{
		ERROROUT("no this param!");
		return -1;
	}
	///find position of devName
	l_temp=l_roomName;
	while(g_other.st_pCmdOther[l_temp]!='*' || g_other.st_pCmdOther[l_temp+1]!='*') l_temp++;
	g_other.st_pCmdOther[l_temp]='\0';
	l_temp++;
	l_temp++;
	l_devName=l_temp;
	while(g_other.st_pCmdOther[l_temp]!='#')	l_temp++;
	g_other.st_pCmdOther[l_temp]='\0';
	///insert
	sprintf(cmdStr,"INSERT INTO devlist VALUES(%d,%d,%d,%d,'%s','%s','%s',0);",l_typeID,l_locationID,l_iconID,l_operID,l_tempStr,&g_other.st_pCmdOther[l_roomName],&g_other.st_pCmdOther[l_devName]);
	if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
	{
		ERROROUT("add Equipment failure.");
		goto FAILED;
	}
	///return
	sqlite3_free(errmsg);
	closeDatabase();
	return 0;

	FAILED:
	sqlite3_free(errmsg);
	closeDatabase();
	return -1;
}
/***updateDev() brief update device in devlist
 * param pos[in], the position of 'struct StControlCmd'
 *
 * retval int,0 if successful or -1 failed
 ***/
int updateDev(int pos)
{
	char* errmsg=NULL;
	int l_devName;
	int l_roomName;
	int l_typeID;
	char l_tempStr[20];
	int l_temp=0;
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	///update
	l_typeID=g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2];
	if(0x01==g_downTask.st_command[pos].param[1])			///operID
	{
		memcpy(l_tempStr,&g_other.st_pCmdOther[0],4);
		l_tempStr[4]=0;
		int l_operID=(int)strtol(l_tempStr,(char**)NULL,16);
		sprintf(cmdStr,"UPDATE devlist SET operID=%d WHERE typeID=%d;",l_operID,l_typeID);
	}
	else if(0x02==g_downTask.st_command[pos].param[1])		///socketID
	{
		while(g_other.st_pCmdOther[l_temp]!='#')	l_temp++;
		g_other.st_pCmdOther[l_temp]='\0';
		sprintf(cmdStr,"UPDATE devlist SET socketID='%s' WHERE typeID=%d;",g_other.st_pCmdOther,l_typeID);
	}
	else if(0x03==g_downTask.st_command[pos].param[1])		///devName
	{
		while(g_other.st_pCmdOther[l_temp]!='#')	l_temp++;
		g_other.st_pCmdOther[l_temp]='\0';
		sprintf(cmdStr,"UPDATE devlist SET devName='%s' WHERE typeID=%d;",g_other.st_pCmdOther,l_typeID);
	}
	else if(0x04==g_downTask.st_command[pos].param[1])		///locationID
	{
		memcpy(l_tempStr,&g_other.st_pCmdOther[0],2);
		l_tempStr[2]=0;
		int l_locationID=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
		sprintf(cmdStr,"UPDATE devlist SET locationID=%d WHERE typeID=%d;",l_locationID,l_typeID);
	}
	else if(0x05==g_downTask.st_command[pos].param[1])		///roomName
	{
		while(g_other.st_pCmdOther[l_temp]!='#')	l_temp++;
		g_other.st_pCmdOther[l_temp]='\0';
		sprintf(cmdStr,"UPDATE devlist SET roomName='%s' WHERE typeID=%d;",g_other.st_pCmdOther,l_typeID);
	}
	else if(0x06==g_downTask.st_command[pos].param[1])		///iconID
	{
		memcpy(l_tempStr,&g_other.st_pCmdOther[0],2);
		l_tempStr[2]=0;
		int l_iconID=(int)strtol(l_tempStr,(char**)NULL,16);
		sprintf(cmdStr,"UPDATE devlist SET iconID=%d WHERE typeID=%d;",l_iconID,l_typeID);
	}
	else if(0x07==g_downTask.st_command[pos].param[1])		///operID+locationID+iconID+socketID+devName+roomName
	{
		memcpy(l_tempStr,&g_other.st_pCmdOther[0],2);	///locationID
		l_tempStr[2]=0;
		int l_locationID=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
		memcpy(l_tempStr,&g_other.st_pCmdOther[2],2);	///iconID
		l_tempStr[2]=0;
		int l_iconID=(unsigned char)strtol(l_tempStr,(char**)NULL,16);
		memcpy(l_tempStr,&g_other.st_pCmdOther[4],4);	///operID
		l_tempStr[4]=0;
		int l_operID=(int)strtol(l_tempStr,(char**)NULL,16);
		memcpy(l_tempStr,&g_other.st_pCmdOther[8],16);	///socketID
		l_tempStr[16]=0;
		l_roomName=24;
		l_temp=l_roomName;
		while(g_other.st_pCmdOther[l_temp]!='*' || g_other.st_pCmdOther[l_temp+1]!='*') l_temp++;
		g_other.st_pCmdOther[l_temp]='\0';
		l_temp++;
		l_temp++;
		l_devName=l_temp;
		while(g_other.st_pCmdOther[l_temp]!='#')	l_temp++;
		g_other.st_pCmdOther[l_temp]='\0';
		sprintf(cmdStr,"UPDATE devlist SET operID=%d,locationID=%d,iconID=%d,socketID='%s',devName='%s',roomName='%s' WHERE typeID=%d;",l_operID,l_locationID,l_iconID,l_tempStr,&g_other.st_pCmdOther[l_devName],&g_other.st_pCmdOther[l_roomName],l_typeID);
	}
	if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
	{
		ERROROUT("update failed.");
		goto FAILED;
	}
	///return
	sqlite3_free(errmsg);
	closeDatabase();
	return 0;

	FAILED:
	sqlite3_free(errmsg);
	closeDatabase();
	return -1;
}
/***delDev() brief delete device from devlist
 * param pos[in], the position of 'struct StControlCmd'
 *
 * retval int,0 if successful or -1 failed
 ***/
int delDev(int pos)
{
	char* errmsg=NULL;
	int l_typeID;
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	///delete
	l_typeID=g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2];
	sprintf(cmdStr,"DELETE FROM devlist WHERE typeID=%d;",l_typeID);
	if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
	{
		ERROROUT("delete Equipment failure!");
		goto FAILED;
	}
	///return
	sqlite3_free(errmsg);
	closeDatabase();
	return 0;

	FAILED:
	sqlite3_free(errmsg);
	closeDatabase();
	return -1;
}
/***addMode() brief add mode
 * param pos[in], the position of 'struct StControlCmd'
 *
 * retval int,0 if successful or -1 failed
 ***/
int addMode(int pos)
{
	char* errmsg=NULL;
	char** l_result = NULL;					///	save the inquiry result
	int l_row = 0;							///	the number of row of inquiry result
	int l_column = 0;						///	the number of column of inquiry result
	int l_typeID;							/// save device's typeID temporarily
	int l_cmdType;							/// save command type  temporarily
	int l_controlValue;						///	save controlValue temporarily
	int l_frequency;						///	save frequency temporaily
	int l_num;								///	save the number of command
	int	l_controlTime;						///	save the controlTime
	char l_tempStr[12];						///	save intermediate data temporarily
	int l_temp = 0;							///	save intermediate data temporarily
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	///see if mode have exist???
	sprintf(cmdStr,"SELECT modeID FROM model WHERE modeID=%d;",g_downTask.st_command[pos].param[1]);
	if(sqlite3_get_table(db,cmdStr,&l_result,&l_row,&l_column,&errmsg))
	{
		ERROROUT("query failed.");
		sqlite3_free(errmsg);
		sqlite3_free_table(l_result);
		goto FAILED;
	}
	sqlite3_free(errmsg);
	sqlite3_free_table(l_result);

	if(0!=l_row)
	{
		ERROROUT("this mode have exist!");
		closeDatabase();
		return -2;           ///the mode exist
	}
	///add mode
	memcpy(l_tempStr,g_other.st_pCmdOther,2);				///command number
	l_tempStr[2]=0;
	l_num=strtol(l_tempStr,(char**)NULL,16);
	//	if(l_num==0)											///permit command-number=0
	//	{
	//		PRINTOUT("%s","It's meaningless for us to add a model which has no order, so just donot do this.");
	//		goto FAILED;
	//	}
	for(int i=0;i<l_num;i++)									///	get the data of command  and insert into table  modtime
	{
		memcpy(l_tempStr,&g_other.st_pCmdOther[i*24+2],6);	///typeID
		l_tempStr[6]=0;
		l_typeID=strtol(l_tempStr,(char**)NULL,16);
		memcpy(l_tempStr,&g_other.st_pCmdOther[i*24+8],2);	///cmdType
		l_tempStr[2]=0;
		l_cmdType=strtol(l_tempStr,(char**)NULL,16);
		memcpy(l_tempStr,&g_other.st_pCmdOther[i*24+10],4);	///param
		l_tempStr[4]=0;
		l_controlValue=strtol(l_tempStr,(char**)NULL,16);
		memcpy(l_tempStr,&g_other.st_pCmdOther[i*24+14],2);	///frequency
		l_tempStr[2]=0;
		l_frequency=strtol(l_tempStr,(char**)NULL,16);
		memcpy(l_tempStr,&g_other.st_pCmdOther[i*24+16],10);///controlTime
		l_tempStr[10]=0;
		l_controlTime=atoi(l_tempStr);
		sprintf(cmdStr,"INSERT INTO modtime(typeID,cmdType,controlVal,controlTime,frequency,remark,modeID) VALUES(%d,%d,%d,%d,%d,'0',%d);",\
				l_typeID,l_cmdType,l_controlValue,l_controlTime,l_frequency,g_downTask.st_command[pos].param[1]);
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("set mode failure.");
			sqlite3_free(errmsg);
			goto FAILED;
		}
		sqlite3_free(errmsg);
	}
	///update 'model'
	l_temp = l_num*24+2;						///	point at the model name in the command
	while(g_other.st_pCmdOther[l_temp]!='#')
	{
		l_temp++;
	}
	g_other.st_pCmdOther[l_temp]=0;
	///	insert modelID,modelName into tabel model
	sprintf(cmdStr,"INSERT INTO model(modeID,name) VALUES(%d,'%s');",g_downTask.st_command[pos].param[1],&g_other.st_pCmdOther[l_num*24+2]);
	if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
	{
		ERROROUT("update model failure.");
		sqlite3_free(errmsg);
		goto FAILED;
	}
	sqlite3_free(errmsg);						///	release the memery possessed by error message
	///return
	closeDatabase();							///	close database
	return 0;									///	quit

	FAILED:										/// if operate failed
	closeDatabase();							///	close database
	return -1;									///	quit
}
/***updateMode() brief update mode
 * param pos[in], the position of 'struct StControlCmd'
 *
 * retval int,0 if successful or -1 failed
 ***/
int updateMode(int pos)
{
	char* errmsg=NULL;
	int l_temp = 0;								///	save intermediate data temporarily
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	///update
	///update 'model'
	while(g_other.st_pCmdOther[l_temp]!='#')  l_temp++;
	g_other.st_pCmdOther[l_temp]=0;
	///	modify the name of model
	sprintf(cmdStr,"UPDATE model SET name='%s' WHERE modeID=%d;",g_other.st_pCmdOther,g_downTask.st_command[pos].param[1]);
	if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
	{
		ERROROUT("update mode failure.");
		goto FAILED;
	}
	sqlite3_free(errmsg);							///	release the memery possessed by error message
	closeDatabase();								///	close database
	return 0;										///	quit

	FAILED:											///	if operate failed
	sqlite3_free(errmsg);							///	release the memery possessed by error message
	closeDatabase();								///	close database
	return -1;										///	quit
}
/***delMode() brief delete mode
 * param pos[in], the position of 'struct StControlCmd'
 *
 * retval int,0 if successful or -1 failed
 ***/
int delMode(int pos)
{
	char* errmsg=NULL;
	int l_controlValue;								///	save the controlValue of command
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	///	the controlValue is 0xfe01,and change it into decimalism (0xfe->254)*256=65024
	l_controlValue=65024+g_downTask.st_command[pos].param[1];
	///	delete the value in the time according controlValue
	sprintf(cmdStr,"DELETE FROM time WHERE controlVal=%d;",l_controlValue);
	if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
	{
		ERROROUT("delete modetime from time table failure.");
		goto FAILED;
	}
	sqlite3_free(errmsg);
	///	delete the value in the model according modelID
	sprintf(cmdStr,"DELETE FROM model WHERE modeID=%d;",g_downTask.st_command[pos].param[1]);
	if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
	{
		ERROROUT("delete mode failure.");
		goto FAILED;
	}
	sqlite3_free(errmsg);
	///	delete the value in the modtime according modelID
	sprintf(cmdStr,"DELETE FROM modtime WHERE modeID=%d;",g_downTask.st_command[pos].param[1]);
	if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
	{
		ERROROUT("delete mode in modtime failure.");
		goto FAILED;
	}
	///return
	sqlite3_free(errmsg);						///	release the memery possessed by error message
	closeDatabase();							///	close database
	return 0;									///	quit

	FAILED:										///	if operate failed
	sqlite3_free(errmsg);						///	release the memery possessed by error messag
	closeDatabase();							///	close database
	return -1;									///	quit
}
/***modelOper() brief get special model-task from database to operate
 * param modeID[in], the ID of special model
 *
 * retval int, 0 if successful or -1 failed
 ***/
int modelOper(int modeID)
{
	char* errmsg=NULL;
	char** l_result = NULL;
	int l_row = 0;
	int l_column = 0;
	int l_comPos;
	int l_temp;
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	if(0>=modeID)						///modeID must greater than 0
		goto FAILED;
	///inquiry
	sprintf(cmdStr,"SELECT typeID,cmdType,controlVal FROM modtime WHERE modeID=%d;",modeID);
	if(sqlite3_get_table(db,cmdStr,&l_result,&l_row,&l_column,&errmsg))
	{
		ERROROUT("query failed.");
		goto FAILED;
	}
	if(0==l_row)
	{
		ERROROUT("no this model in database!");
		goto FAILED;
	}
	sem_wait(&g_downTask.st_emtpy.st_sem);///!!!!!
	sem_wait(&g_downTask.st_task.st_sem);///!!!!!
	for(int i=1;i<l_row+1;i++)
	{
		l_comPos=getElem(g_downTask.st_emtpy);

		if(-1==l_comPos)
		{
			ERROROUT("NO enough struct for mode operate!");
			sem_post(&g_downTask.st_emtpy.st_sem);///!!!!!
			sem_post(&g_downTask.st_task.st_sem);///!!!!!
			goto FAILED;
		}
		l_temp=atoi(l_result[i*l_column]);
		g_downTask.st_command[l_comPos].typeID[0]=l_temp>>16 & 0xff;
		g_downTask.st_command[l_comPos].typeID[1]=l_temp>>8 & 0xff;
		g_downTask.st_command[l_comPos].typeID[2]=l_temp & 0xff;
		g_downTask.st_command[l_comPos].cmdType=atoi(l_result[i*l_column+1]);
		l_temp=atoi(l_result[i*l_column+2]);
		g_downTask.st_command[l_comPos].param[0]=l_temp>>8 & 0xff;
		g_downTask.st_command[l_comPos].param[1]=l_temp & 0xff;
		g_downTask.st_command[l_comPos].otherParam=0;
		g_downTask.st_command[l_comPos].priority=prio_modeltask;

		insertElem(g_downTask.st_task,l_comPos);
	}
	sem_post(&g_downTask.st_emtpy.st_sem);///!!!!!
	sem_post(&g_downTask.st_task.st_sem);///!!!!!
	///return
	sqlite3_free_table(l_result);
	sqlite3_free(errmsg);
	closeDatabase();
	return 0;

	FAILED:
	sqlite3_free_table(l_result);
	sqlite3_free(errmsg);
	closeDatabase();
	return -1;
}
/***modelQuery() brief query model from database to webservice
 * param pos[in], the position of 'struct StControlCmd'
 *
 * retval int, 0 if successful or -1 failed
 ***/
int modelQuery(int pos)
{
	char* errmsg=NULL;
	char** l_result=NULL;
	int l_row=0;
	int l_column=0;
	int l_sendNum=0;
	char l_tempStr[12];
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	///get
	if(0x00==g_downTask.st_command[pos].param[1])		///mode-name-list inquriy
	{
		sprintf(cmdStr,"SELECT modeID,name FROM model;");
		if(sqlite3_get_table(db,cmdStr,&l_result,&l_row,&l_column,&errmsg))
		{
			ERROROUT("query failed.");
			goto FAILED;
		}
		for(int l_i=1;l_i<l_row+1;l_i++)
		{												///modeID
			sprintf(l_tempStr,"%02x",atoi(l_result[l_i*l_column]));
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
			g_other.st_pCmdOther[l_sendNum++]=';';
			///name
			memcpy(&g_other.st_pCmdOther[l_sendNum],l_result[l_i*l_column+1],strlen(l_result[l_i*l_column+1]));
			l_sendNum+=strlen(l_result[l_i*l_column+1]);
			g_other.st_pCmdOther[l_sendNum++]=';';
			g_other.st_pCmdOther[l_sendNum++]='@';
		}
		g_other.st_pCmdOther[l_sendNum++]='#';
		g_other.st_pCmdOther[l_sendNum]='\0';
		sqlite3_free_table(l_result);
		sqlite3_free(errmsg);
	}
	else												///special mode inquiry
	{
		sprintf(cmdStr,"SELECT typeID,cmdType,controlVal,controlTime,frequency FROM modtime WHERE modeID=%d;",g_downTask.st_command[pos].param[1]);
		if(sqlite3_get_table(db,cmdStr,&l_result,&l_row,&l_column,&errmsg))
		{
			ERROROUT("query failed.");
			goto FAILED;
		}
		for(int i=1;i<l_row+1;i++)
		{
			sprintf(l_tempStr,"%06x",atoi(l_result[i*l_column]));
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[2];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[3];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[4];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[5];
			g_other.st_pCmdOther[l_sendNum++]=';';///typeID

			sprintf(l_tempStr,"%02x",atoi(l_result[i*l_column+1]));
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
			g_other.st_pCmdOther[l_sendNum++]=';';///cmdtype

			sprintf(l_tempStr,"%04x",atoi(l_result[i*l_column+2]));
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[2];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[3];
			g_other.st_pCmdOther[l_sendNum++]=';';///controlval

			sprintf(l_tempStr,"%02x",atoi(l_result[i*l_column+4]));
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
			g_other.st_pCmdOther[l_sendNum++]=';';///frequency

			sprintf(l_tempStr,"%010d",atoi(l_result[i*l_column+3]));
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[0];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[1];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[2];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[3];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[4];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[5];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[6];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[7];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[8];
			g_other.st_pCmdOther[l_sendNum++]=l_tempStr[9];
			g_other.st_pCmdOther[l_sendNum++]=';';///controlTime

			g_other.st_pCmdOther[l_sendNum++]='@';
		}
		g_other.st_pCmdOther[l_sendNum++]='#';
		g_other.st_pCmdOther[l_sendNum]='\0';
		sqlite3_free_table(l_result);
		sqlite3_free(errmsg);
	}

	///return
	closeDatabase();
	return 0;

	FAILED:
	sqlite3_free_table(l_result);
	sqlite3_free(errmsg);
	closeDatabase();
	return -1;
}
/***tmTaskQuery() brief get special time-task or model task from sqlite
 * param pos[in], the position of 'struct StControlCmd'
 * param devPos[in],the position of 'struct StEquipment'
 * param insertPos[in], the insert position of 'g_dataTrans.st_sendService'
 *
 * retval int,the insert position of 'g_dataTrans.st_sendService' if successful or -1 failed.
 ***/
int tmTaskQuery(int pos,int devPos,int insertPos)
{
	char* errmsg=NULL;
	char** l_result=NULL;
	int l_row=0;
	int l_column=0;
	char l_tempStr[12];
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	//////inquriy
	///all device's time-task
	if(-1==devPos)
		sprintf(cmdStr,"SELECT cmdType,controlVal,controlTime,frequency,remark,typeID FROM time;");
	///special device's time-task
	else
		sprintf(cmdStr,"SELECT cmdType,controlVal,controlTime,frequency,remark FROM time WHERE typeID=%d;",g_devList.st_equipArray[devPos].typeID);
	if(sqlite3_get_table(db,cmdStr,&l_result,&l_row,&l_column,&errmsg))
	{
		ERROROUT("query failed.");
		goto FAILED;
	}
	struct timeval l_tv;
	if(-1==gettimeofday(&l_tv,NULL))
	{
		ERROROUT("get-time failed!");
		goto FAILED;
	}
	for(int i=1;i<l_row+1;i++)
	{
		if(atoi(l_result[i*l_column+2])<l_tv.tv_sec && 0==atoi(l_result[i*l_column+3]))
			continue;

		if(-1==devPos)
			sprintf(l_tempStr,"%06x",atoi(l_result[i*l_column+5]));
		else
			sprintf(l_tempStr,"%06x",g_devList.st_equipArray[devPos].typeID);

		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
		g_other.st_pCmdOther[insertPos++]=';';///typeID

		sprintf(l_tempStr,"%02x",atoi(l_result[i*l_column]));
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=';';///cmdtype

		sprintf(l_tempStr,"%04x",atoi(l_result[i*l_column+1]));
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
		g_other.st_pCmdOther[insertPos++]=';';///controlVal

		sprintf(l_tempStr,"%02x",atoi(l_result[i*l_column+3]));
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=';';///frequency

		sprintf(l_tempStr,"%010d",atoi(l_result[i*l_column+2]));
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[8];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[9];
		g_other.st_pCmdOther[insertPos++]=';';///controlTime

		memcpy(&g_other.st_pCmdOther[insertPos],l_result[i*l_column+4],strlen(l_result[i*l_column+4]));
		insertPos+=strlen(l_result[i*l_column+4]);
		g_other.st_pCmdOther[insertPos++]=';';///remark

		g_other.st_pCmdOther[insertPos++]='@';
	}
	///return
	sqlite3_free_table(l_result);
	sqlite3_free(errmsg);
	closeDatabase();
	return insertPos;

	FAILED:
	sqlite3_free_table(l_result);
	sqlite3_free(errmsg);
	closeDatabase();
	return -1;
}
/***addTask() brief add special timetask or model task to sqlite
 * param pos[in], the position of 'struct StControlCmd'
 *
 * retval int,0 if successful or -1 failed
 ***/
int addTask(int pos)
{
	char* errmsg=NULL;
	int l_typeID;									///	save typeID
	int l_cmdType;									///	save command type
	int l_controlValue;								///	save control value
	int l_frequency;								///	save frequency
	char l_tempStr[12];								///	save intermediate data temporarily
	int l_temp;										///	save intermediate data temporarily
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	///add
	l_typeID=g_downTask.st_command[pos].typeID[0]*256*256+g_downTask.st_command[pos].typeID[1]*256+g_downTask.st_command[pos].typeID[2];
	l_cmdType=g_downTask.st_command[pos].cmdType;
	l_controlValue=g_downTask.st_command[pos].param[0]*256+g_downTask.st_command[pos].param[1];

	memcpy(l_tempStr,g_other.st_pCmdOther,2);	///frequency
	l_tempStr[2]=0;
	l_frequency=strtol(l_tempStr,(char**)NULL,16);

	l_temp=12;									///remark
	while('#'!=g_other.st_pCmdOther[l_temp])	l_temp++;
	g_other.st_pCmdOther[l_temp]=0;
	///	insert the value in the command into time
	sprintf(cmdStr,"INSERT INTO time(typeID,cmdType,controlVal,controlTime,frequency,remark) VALUES(%d,%d,%d,%d,%d,'%s');",\
			l_typeID,l_cmdType,l_controlValue,g_downTask.st_command[pos].controlTime,l_frequency,g_other.st_pCmdOther+12);
	if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
	{
		ERROROUT("add time-task failure.");
		goto FAILED;
	}
	///return
	sqlite3_free(errmsg);								///	release the memery possessed by error message
	closeDatabase();									///	close database
	return 0;											///	quit

	FAILED:												///	if operate failed
	sqlite3_free(errmsg);								///	release the memery possessed by error message
	closeDatabase();									///	close database
	return -1;											///	quit
}
/***delTask() brief del special timetask or model task from database
 * param devPos[in],the position of 'struct StEquipment'
 *
 * retval int,0 if successful or -1 failed
 ***/
int delTask(int devpos)
{
	char* errmsg=NULL;
	int l_typeID;
	int l_controlValue;
	int l_frequency;
	int l_controlTime;
	char l_tempStr[12];
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	///delete
	if(-1!=devpos)
		l_typeID=g_devList.st_equipArray[devpos].typeID;	///l_typeID

	memcpy(l_tempStr,&g_other.st_pCmdOther[0],4);			///controlVal
	l_tempStr[4]=0;
	l_controlValue=strtol(l_tempStr,(char**)NULL,16);

	memcpy(l_tempStr,&g_other.st_pCmdOther[4],2);			///frequency
	l_tempStr[2]=0;
	l_frequency=strtol(l_tempStr,(char**)NULL,16);

	memcpy(l_tempStr,&g_other.st_pCmdOther[6],10);			///controlTime
	l_tempStr[10]=0;
	l_controlTime=atoi(l_tempStr);

	if(-1!=devpos)
		sprintf(cmdStr,"DELETE FROM time WHERE (typeID=%d AND cmdType=1 AND controlVal=%d AND frequency=%d AND controlTime=%d);",\
				l_typeID,l_controlValue,l_frequency,l_controlTime);
	else
		sprintf(cmdStr,"DELETE FROM time WHERE (cmdType=1 AND controlVal=%d AND frequency=%d AND controlTime=%d);",\
				l_controlValue,l_frequency,l_controlTime);
	if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
	{
		ERROROUT("delete time-task failure.");
		goto FAILED;
	}
	///return
	sqlite3_free(errmsg);
	closeDatabase();
	return 0;

	FAILED:
	sqlite3_free(errmsg);
	closeDatabase();
	return -1;
}
/***modifyTask() brief modify special timetask or model task in database
 * param devPos[in],the position of 'struct StEquipment'
 *
 * retval int,0 if successful or -1 failed
 ***/
int modifyTask(int devpos)
{
	char* errmsg=NULL;
	char l_tempStr[12];
	int l_typeID,l_controlVal,l_controlTime,l_oldcontrolVal,l_oldcontrolTime,l_oldfre,l_newfre,l_remark;
	struct timeval l_tv;
	int l_temp;
	int l_tmpTime;

	if(-1!=devpos)
		l_typeID=g_devList.st_equipArray[devpos].typeID;	///typeID

	memcpy(l_tempStr,&g_other.st_pCmdOther[0],4);		///old-controlVal
	l_tempStr[4]=0;
	l_oldcontrolVal=strtol(l_tempStr,(char**)NULL,16);
	memcpy(l_tempStr,&g_other.st_pCmdOther[16],4);		///new-controlVal
	l_tempStr[4]=0;
	l_controlVal=strtol(l_tempStr,(char**)NULL,16);

	memcpy(l_tempStr,&g_other.st_pCmdOther[4],2);		///old-frequency
	l_tempStr[2]=0;
	l_oldfre=strtol(l_tempStr,(char**)NULL,16);
	memcpy(l_tempStr,&g_other.st_pCmdOther[20],2);		///new-frequency
	l_tempStr[2]=0;
	l_newfre=strtol(l_tempStr,(char**)NULL,16);

	memcpy(l_tempStr,&g_other.st_pCmdOther[6],10);		///old-controlTime
	l_tempStr[10]=0;
	l_oldcontrolTime=atoi(l_tempStr);
	memcpy(l_tempStr,&g_other.st_pCmdOther[22],10);		///new-controlTime
	l_tempStr[10]=0;
	l_controlTime=atoi(l_tempStr);

	l_remark=32;										///new-remark
	l_temp=l_remark;
	while(g_other.st_pCmdOther[l_temp]!='#') l_temp++;
	g_other.st_pCmdOther[l_temp]=0;

	if(-1==gettimeofday(&l_tv,NULL))
	{
		ERROROUT("get-time failed!");
		goto FAILED;
	}
	///outdate task should not be modified
	///adjust time stamp
	l_tmpTime=getTodayZero(l_tv.tv_sec);
	if(l_oldcontrolTime<=l_tv.tv_sec && 0x00==l_oldfre)
	{
		return -1;
	}
	else if(0x00!=l_oldfre && l_oldcontrolTime<l_tv.tv_sec && l_controlTime>l_tv.tv_sec && l_controlTime<(l_tmpTime+3600*24))
		///if outdate task but periodic, and in today, put into memory
	{
		sem_wait(&g_downTask.st_emtpy.st_sem);///!!!!!
		l_temp=getElem(g_downTask.st_emtpy);
		sem_post(&g_downTask.st_emtpy.st_sem);///!!!!!
		if(-1==l_temp)
			return -1;
		if(-1!=devpos)
		{
			g_downTask.st_command[l_temp].typeID[0]=g_devList.st_equipArray[devpos].typeID>>16 & 0xff;
			g_downTask.st_command[l_temp].typeID[1]=g_devList.st_equipArray[devpos].typeID>>8 & 0xff;
			g_downTask.st_command[l_temp].typeID[2]=g_devList.st_equipArray[devpos].typeID & 0xff;
		}
		else
		{
			g_downTask.st_command[l_temp].typeID[0]=0;
			g_downTask.st_command[l_temp].typeID[1]=0;
			g_downTask.st_command[l_temp].typeID[2]=0;
		}
		g_downTask.st_command[l_temp].cmdType=0x01;
		g_downTask.st_command[l_temp].otherParam=0x00;
		g_downTask.st_command[l_temp].param[0]=l_controlVal>>8 & 0xff;
		g_downTask.st_command[l_temp].param[1]=l_controlVal & 0xff;
		g_downTask.st_command[l_temp].controlTime=l_controlTime;
		g_downTask.st_command[l_temp].priority=prio_timetask;
		sem_wait(&g_downTask.st_timeTask.st_sem);///!!!!!
		insertElem(g_downTask.st_timeTask,l_temp);
		sem_post(&g_downTask.st_timeTask.st_sem);///!!!!!
	}
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	if(-1==devpos)
		sprintf(cmdStr,"UPDATE time SET controlVal=%d,frequency=%d,controlTime=%d,remark='%s' WHERE (controlVal=%d AND frequency=%d AND controlTime=%d);",\
				l_controlVal,l_newfre,l_controlTime,&g_other.st_pCmdOther[l_remark],l_oldcontrolVal,l_oldfre,l_oldcontrolTime);
	else
		sprintf(cmdStr,"UPDATE time SET controlVal=%d,frequency=%d,controlTime=%d,remark='%s' WHERE (typeID=%d AND controlVal=%d AND frequency=%d AND controlTime=%d);",\
				l_controlVal,l_newfre,l_controlTime,&g_other.st_pCmdOther[l_remark],l_typeID,l_oldcontrolVal,l_oldfre,l_oldcontrolTime);
	if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
	{
		ERROROUT("delete time-task failure.");
		goto FAILED;
	}
	///return
	sqlite3_free(errmsg);
	closeDatabase();
	return 0;

	FAILED:
	sqlite3_free(errmsg);
	closeDatabase();
	return -1;
}
/***updateTask() brief update timetask to g_downTask.st_timeTask everyday in 00:00
 * param void
 *
 * retval int,0 if successful or -1 failed
 ***/
int updateTask00()
{
	char* errmsg=NULL;
	char** l_result=NULL;
	int l_row=0;
	int l_column=0;
	int l_comPos;
	int l_typeID,l_contTime,l_frequency;
	int l_temp;
	int l_new=2147483647;					///give 'l_new' here the max value of 'int'
	int l_tmpTime;
	int l_tmpcontTime;
//	delOutdatedData();         			///del out date elec data
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	///inquiry
	sprintf(cmdStr,"SELECT typeID,cmdType,controlVal,controlTime,frequency FROM time;");
	if(sqlite3_get_table(db,cmdStr,&l_result,&l_row,&l_column,&errmsg))
	{
		ERROROUT("query failed.");
		sqlite3_free(errmsg);
		sqlite3_free_table(l_result);
		goto FAILED;
	}
	struct timeval l_tv;
	if(-1==gettimeofday(&l_tv,NULL))
	{
		ERROROUT("get-time failed!");
		sqlite3_free(errmsg);
		sqlite3_free_table(l_result);
		goto FAILED;
	}
	///adjust time stamp
	l_tmpTime=getTodayZero(l_tv.tv_sec);
	sqlite3_free(errmsg);
	for(int i=1;i<l_row+1;i++)
	{
		l_new=2147483647;
		l_contTime=atoi(l_result[i*l_column+3]);
		l_typeID=atoi(l_result[i*l_column]);
		///adjust MOD
		l_tmpcontTime=l_contTime%(3600*24);
		if(l_tmpcontTime<57600)
		{
			l_tmpcontTime+=28800;
		}
		else
		{
			l_tmpcontTime-=57600;
		}
		if(l_contTime<l_tv.tv_sec)
		{
			l_frequency=atoi(l_result[i*l_column+4]);	///frequency
			if(0x00==l_frequency)						///someday
			{
				sprintf(cmdStr,"DELETE FROM time WHERE (typeID=%d AND controlTime=%d AND frequency=%d);",l_typeID,l_contTime,l_frequency);
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("delete overdue time failed!");
					//goto FAILED;
				}
				sqlite3_free(errmsg);
			}
			else if(0x01==l_frequency)					///everyday
			{
				l_temp=l_tmpcontTime+l_tmpTime;
//				sprintf(cmdStr,"UPDATE time SET controlTime=%d WHERE (typeID=%d AND controlTime=%d AND frequency=%d);",l_temp,l_typeID,l_contTime,l_frequency);
//				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
//				{
//					ERROROUT("update time's controlTime failed!");
//					//goto FAILED;
//				}
//				sqlite3_free(errmsg);
				l_contTime=l_temp;
				if(l_contTime>l_tv.tv_sec)
					goto COPYTOMEM;
			}
			else
			{
				struct timeval l_tmpTv;
				l_tmpTv.tv_sec=l_tv.tv_sec+8*3600;
				int l_day=localtime(&l_tmpTv.tv_sec)->tm_wday;

				if(1==(l_frequency >>7 & 0x1))			///every Sunday-0
				{
					l_temp=l_tmpTime-3600*24*l_day+l_tmpcontTime;
					if(l_temp<=l_contTime || l_temp<l_tv.tv_sec)
						l_temp+=3600*24*7;
					if(l_new>l_temp)
						l_new=l_temp;
				}
				if(1==(l_frequency >> 1 & 0x1))			///every Monday-1
				{
					l_temp=l_tmpTime-3600*24*(l_day-1)+l_tmpcontTime;
					if(l_temp<=l_contTime || l_temp<l_tv.tv_sec)
						l_temp+=3600*24*7;
					if(l_new>l_temp)
						l_new=l_temp;
				}
				if(1==(l_frequency >>2 & 0x1))			///every Tuesday-2
				{
					l_temp=l_tmpTime-3600*24*(l_day-2)+l_tmpcontTime;
					if(l_temp<=l_contTime || l_temp<l_tv.tv_sec)
						l_temp+=3600*24*7;
					if(l_new>l_temp)
						l_new=l_temp;
				}
				if(1==(l_frequency >> 3 & 0x1))			///every Wednesday-3
				{
					l_temp=l_tmpTime-3600*24*(l_day-3)+l_tmpcontTime;
					if(l_temp<=l_contTime || l_temp<l_tv.tv_sec)
						l_temp+=3600*24*7;
					if(l_new>l_temp)
						l_new=l_temp;
				}
				if(1==(l_frequency >> 4 & 0x1))			///every Thursday-4
				{
					l_temp=l_tmpTime-3600*24*(l_day-4)+l_tmpcontTime;
					if(l_temp<=l_contTime || l_temp<l_tv.tv_sec)
						l_temp+=3600*24*7;
					if(l_new>l_temp)
						l_new=l_temp;
				}
				if(1==(l_frequency >> 5 & 0x1))			///every Friday-5
				{
					l_temp=l_tmpTime-3600*24*(l_day-5)+l_tmpcontTime;
					if(l_temp<=l_contTime || l_temp<l_tv.tv_sec)
						l_temp+=3600*24*7;
					if(l_new>l_temp)
						l_new=l_temp;
				}
				if(1==(l_frequency >> 6 & 0x1))			///every Saturday-6
				{
					l_temp=l_tmpTime-3600*24*(l_day-6)+l_tmpcontTime;
					if(l_temp<=l_contTime || l_temp<l_tv.tv_sec)
						l_temp+=3600*24*7;
					if(l_new>l_temp)
						l_new=l_temp;
				}
//				sprintf(cmdStr,"UPDATE time SET controlTime=%d WHERE (typeID=%d AND controlTime=%d AND frequency=%d);",l_new,l_typeID,l_contTime,l_frequency);
//				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
//				{
//					ERROROUT("update time's controlTime failed!");
//					//goto FAILED;
//				}
//				sqlite3_free(errmsg);
				if(l_new>l_tv.tv_sec && l_new<(l_tmpTime+3600*24))
				{
					l_contTime=l_new;
					goto COPYTOMEM;
				}
			}

			continue;
		}
		else if(l_contTime>(l_tmpTime+3600*24))
			continue;
		COPYTOMEM:
		///apply
		sem_wait(&g_downTask.st_emtpy.st_sem);///!!!!!
		l_comPos=getElem(g_downTask.st_emtpy);
		sem_post(&g_downTask.st_emtpy.st_sem);///!!!!!
		if(-1==l_comPos)
		{
			sqlite3_free_table(l_result);
			goto FAILED;
		}
		///make format
		l_temp=atoi(l_result[i*l_column]);
		g_downTask.st_command[l_comPos].typeID[0]=l_temp>>16 & 0xff;
		g_downTask.st_command[l_comPos].typeID[1]=l_temp>>8 & 0xff;
		g_downTask.st_command[l_comPos].typeID[2]=l_temp & 0xff;
		l_temp=atoi(l_result[i*l_column+1]);
		g_downTask.st_command[l_comPos].cmdType=l_temp;
		l_temp=atoi(l_result[i*l_column+2]);
		g_downTask.st_command[l_comPos].param[0]=l_temp>>8 & 0xff;
		g_downTask.st_command[l_comPos].param[1]=l_temp & 0xff;
		g_downTask.st_command[l_comPos].controlTime=l_contTime;
		g_downTask.st_command[l_comPos].priority=prio_timetask;
		///insert
		sem_wait(&g_downTask.st_timeTask.st_sem);///!!!!!
		insertElem(g_downTask.st_timeTask,l_comPos);
		sem_post(&g_downTask.st_timeTask.st_sem);///!!!!!
	}
	///return
	sqlite3_free_table(l_result);
	closeDatabase();
	return 0;

	FAILED:
	closeDatabase();
	return -1;
}
/***inquiryElecPower() brief get special device's hour power, and put into 'g_dataTrans.st_sendService'
 * param pos[in], the position of 'struct StControlCmd'
 * param devPos[in],the position of 'struct StEquipment'
 * param insertPos[in], the insert position of 'g_dataTrans.st_sendService'
 *
 * retval int,the insert position of 'g_dataTrans.st_sendService' if successful or -1 failed.
 ***/
int inquiryElecPower(int pos, int devpos, int insertPos)
{
	char* errmsg=NULL;
	char** l_result = NULL;
	int l_row = 0;
	int l_column = 0;
	char l_tempStr[12];
	int g_elecBegin;
	int g_elecEnd;
	int overElec_flag=0;
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	///inquiry
	if(g_other.st_pCmdOther[insertPos-1]=='#')
	{
		memcpy(l_tempStr, g_other.st_pCmdOther, 10);
		l_tempStr[10]='\0';
		g_elecBegin=atoi(l_tempStr);
//		memcpy(l_tempStr, &g_other.st_pCmdOther[10], 10);
//		l_tempStr[10]='\0';
//		g_elecEnd=atoi(l_tempStr);
	}
	///delete data had success upload
    if(g_elecBegin!=0)
    {
    	sprintf(cmdStr, "DELETE FROM power WHERE hourTime <= %d;",g_elecBegin);
    	if(sqlite3_exec(db, cmdStr, NULL, NULL, &errmsg))
    	{
    		ERROROUT("delete success upload data failed.");
    		goto FAILED;
    	}
    	sqlite3_free(errmsg);
    	///sleep
    	myDelay(100);	///???bug???sleep 100 ms
    }
	///	get data
	sprintf(cmdStr, "SELECT hourTime,data FROM power;");
	if(sqlite3_get_table(db, cmdStr, &l_result, &l_row, &l_column, &errmsg))
	{
		ERROROUT("query failed.");
		goto FAILED;
	}
	///get current time
	struct timeval l_tv;
	if(-1==gettimeofday(&l_tv, NULL))
	{
		ERROROUT("get-time failed!");
		return -1;
	}
	///count begin and end time
	if(l_row ==0)
	{
		overElec_flag=0;
		g_elecBegin=g_elecEnd=0;
	}
	else if (l_row<ELEC_UP_NUM+1)
	{
		overElec_flag=0;
		g_elecBegin=atoi(l_result[1*l_column]);
		g_elecEnd=atoi(l_result[l_row*l_column]);
	}
	else
	{
		overElec_flag=1;
		int l_count1=0;
		int l_count2=0;
		g_elecBegin=atoi(l_result[1*l_column]);
		g_elecEnd=atoi(l_result[ELEC_UP_NUM*l_column]);
		for(int i=1;i<ELEC_UP_NUM;i++)
		{
			if (atoi(l_result[(ELEC_UP_NUM-i)*l_column])==g_elecEnd)
			{
				l_count1++;
			}
			else
			{
				break;
			}
			if (atoi(l_result[(ELEC_UP_NUM+i)*l_column])==g_elecEnd)
			{
				l_count2++;
			}
			else
			{
				break;
			}
		}
		if (l_count1<l_count2)
		{
			g_elecEnd=atoi(l_result[(ELEC_UP_NUM-l_count1)*l_column]);
		}
		else
			g_elecEnd=atoi(l_result[(ELEC_UP_NUM+l_count2)*l_column]);
	}
	sqlite3_free(errmsg);
	if(-1==devpos)		///inquiry all device's hour elec-power
	{
		for(int i=g_devList.st_equip.head;i!=-1;i=g_devList.st_equipArray[i].next)
		{
			///	typeID
			sprintf(l_tempStr, "%06x", g_devList.st_equipArray[i].typeID);
			g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
			g_other.st_pCmdOther[insertPos++]=';';
			///	socketID
			sprintf(l_tempStr,"%02x%02x%02x%02x",g_devList.st_equipArray[i].equipmentID[0],g_devList.st_equipArray[i].equipmentID[1],g_devList.st_equipArray[i].equipmentID[2],g_devList.st_equipArray[i].equipmentID[3]);
			g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
			sprintf(l_tempStr,"%02x%02x%02x%02x",g_devList.st_equipArray[i].equipmentID[4],g_devList.st_equipArray[i].equipmentID[5],g_devList.st_equipArray[i].equipmentID[6],g_devList.st_equipArray[i].equipmentID[7]);
			g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
			g_other.st_pCmdOther[insertPos++]=';';

			///	get data
			sprintf(cmdStr, "SELECT hourTime,data FROM power WHERE typeID = %d AND (hourTime BETWEEN %d AND %d);", g_devList.st_equipArray[i].typeID,g_elecBegin,g_elecEnd);
			if(sqlite3_get_table(db, cmdStr, &l_result, &l_row, &l_column, &errmsg))
			{
				ERROROUT("query failed.");
				goto FAILED;
			}
			///	hourtime and elec-power
			for(int j=1;j<l_row+1;j++)
			{
				sprintf(l_tempStr ,"%010d", atoi(l_result[j*l_column]));
				g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[8];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[9];
				g_other.st_pCmdOther[insertPos++]=';';
				sprintf(l_tempStr, "%08f", atof(l_result[j*l_column+1]));
				g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
				g_other.st_pCmdOther[insertPos++]=';';
			}
			if (l_row<1)
			{
				g_other.st_pCmdOther[insertPos++]=';';
				g_other.st_pCmdOther[insertPos++]=';';
			}
			g_other.st_pCmdOther[insertPos++]='@';				///	seperate from devices
			sqlite3_free(errmsg);
		}
		g_other.st_pCmdOther[insertPos++]='#';              /// add g_elecEnd value for socket
		sprintf(l_tempStr ,"%010d", g_elecEnd);
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[8];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[9];
		g_other.st_pCmdOther[insertPos++]='#';              /// add uploap over flag
		sprintf(l_tempStr ,"%02d", overElec_flag);
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
	}
	else							/// inquiry a device's hour elec-power
	{
		sprintf(l_tempStr, "%06x", g_devList.st_equipArray[devpos].typeID);
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
		g_other.st_pCmdOther[insertPos++]=';';

		///	socketID
		sprintf(l_tempStr,"%02x%02x%02x%02x",g_devList.st_equipArray[devpos].equipmentID[0],g_devList.st_equipArray[devpos].equipmentID[1],g_devList.st_equipArray[devpos].equipmentID[2],g_devList.st_equipArray[devpos].equipmentID[3]);
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
		sprintf(l_tempStr,"%02x%02x%02x%02x",g_devList.st_equipArray[devpos].equipmentID[4],g_devList.st_equipArray[devpos].equipmentID[5],g_devList.st_equipArray[devpos].equipmentID[6],g_devList.st_equipArray[devpos].equipmentID[7]);
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
		g_other.st_pCmdOther[insertPos++]=';';

		///	 get data
		sprintf(cmdStr, "SELECT hourTime,data FROM power WHERE typeID=%d AND (hourTime BETWEEN %d AND %d);", g_devList.st_equipArray[devpos].typeID, g_elecBegin, g_elecEnd);
		if(sqlite3_get_table(db, cmdStr, &l_result, &l_row, &l_column, &errmsg))
		{
			ERROROUT("query failed.");
			goto FAILED;
		}
		///	hourtime and elec-power
		for(int i=1;i<l_row+1;i++)
		{
			sprintf(l_tempStr, "%010d", atoi(l_result[i*l_column]));
			g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[8];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[9];
			g_other.st_pCmdOther[insertPos++]=';';
			sprintf(l_tempStr, "%08f", atof(l_result[i*l_column+1]));
			g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
			g_other.st_pCmdOther[insertPos++]=';';
		}
		if (l_row<1)
		{
			g_other.st_pCmdOther[insertPos++]=';';
			g_other.st_pCmdOther[insertPos++]=';';
		}
		g_other.st_pCmdOther[insertPos++]='@';

		g_other.st_pCmdOther[insertPos++]='#';              /// add g_elecEnd value for socket
		sprintf(l_tempStr ,"%010d", g_elecEnd);
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[8];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[9];
		sprintf(l_tempStr ,"%02d", overElec_flag);
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		sqlite3_free(errmsg);
	}
	///return
	sqlite3_free_table(l_result);
	closeDatabase();
	return insertPos;

	FAILED:
	sqlite3_free_table(l_result);
	sqlite3_free(errmsg);
	closeDatabase();
	return -1;
}
/***saveElecPower() brief save elec in table of "hour" and if necessary delete outdated elec-power
 * param devPos[in],the equipment position in g_devList.st_equipArray
 * param data[in], total power from socket(make sure, it's just total sum from socket's product, not this hour's quantity)
 *
 * retval int, 0 if successful or -1 failed
 ***/
int saveElecPower(int devPos, double data)
{
	char* errmsg=NULL;
	char **l_result = NULL;
	int l_row;
	int l_column;
	char l_tempStr[12];
	int l_time;
	double l_data;

	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	struct timeval l_tv;
	if(-1==gettimeofday(&l_tv, NULL))
	{
		ERROROUT("get-time failed!");
		return -1;
	}
	if(equ_no==g_devList.st_equipArray[devPos].firstElec)
	{
		///save
		l_time=(l_tv.tv_sec-ELECGETPOINT-1)/3600*3600;
		sprintf(l_tempStr, "%d", g_devList.st_equipArray[devPos].typeID);
		///see if data have exist
		sprintf(cmdStr, "SELECT data FROM power WHERE typeID=%d AND hourTime=%d;", g_devList.st_equipArray[devPos].typeID,l_time);
		if(sqlite3_get_table(db, cmdStr, &l_result, &l_row, &l_column, &errmsg))
		{
			ERROROUT("query failed.");
			sqlite3_free_table(l_result);
			goto FAILED;
		}
		if(l_row>0)							///if data have exist, just return
		{
			PRINTOUT("%s","The data has existed.");
			goto OK;
		}
		sqlite3_free(errmsg);
		///sleep
		myDelay(100);	///???bug???sleep 160 ms
		///insert
		l_data = data-g_devList.st_equipArray[devPos].lastHourElecQuan;
		if (l_data<0)
			l_data=0;
		sprintf(cmdStr, "INSERT INTO power VALUES('%s',%d,%f);",l_tempStr, l_time, l_data);
		if(sqlite3_exec(db, cmdStr, NULL, NULL, &errmsg))
		{
			ERROROUT("save data failed.");
			goto FAILED;
		}
		sqlite3_free(errmsg);
		///sleep
		myDelay(150);	///???bug???sleep 150 ms
		///insert
		sprintf(cmdStr, "INSERT INTO tmppower VALUES('%s',%d,%f);", l_tempStr, l_time, data);
		if(sqlite3_exec(db, cmdStr, NULL, NULL, &errmsg))
		{
			ERROROUT("save data failed.");
			goto FAILED;
		}
		sqlite3_free(errmsg);
	}
	else
	{
		g_devList.st_equipArray[devPos].firstElec = equ_no;
	}
	///sleep
	myDelay(50);	///???bug???sleep 50 ms
	///update 'devlist'
	sprintf(cmdStr, "UPDATE devlist SET elecData='%f' WHERE typeID=%d;",data,g_devList.st_equipArray[devPos].typeID);
	if(sqlite3_exec(db, cmdStr, NULL, NULL, &errmsg))
	{
		ERROROUT("update devlist elecData failed!");
		//goto FAILED;					///just ignore
	}
	///update memory
	g_devList.st_equipArray[devPos].lastHourElecQuan = data;

	///return
	OK:
	sqlite3_free_table(l_result);
	sqlite3_free(errmsg);
	closeDatabase();
	return 0;

	FAILED:
	sqlite3_free(errmsg);
	closeDatabase();
	return -1;
}
/***inquiryActivePower() brief get special device's 10min active power
 * param pos[in], the position of 'struct StControlCmd'
 * param devPos[in],the position of 'struct StEquipment'
 * param insertPos[in], the insert position of 'g_dataTrans.st_sendService'
 *
 * retval int,the insert position of 'g_dataTrans.st_sendService' if successful or -1 failed.
 ***/
int inquiryActivePower(int pos, int devpos, int insertPos)
{

	char* errmsg=NULL;
	char** l_result = NULL;
	int l_row = 0;
	int l_column = 0;
	char l_tempStr[12];
	int g_elecBegin;
	int g_elecEnd;
	int overActive_flag=0;
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	///inquiry
	if(g_other.st_pCmdOther[insertPos-1]=='#')
	{
		memcpy(l_tempStr, g_other.st_pCmdOther, 10);
		l_tempStr[10]='\0';
		g_elecBegin=atoi(l_tempStr);
//		memcpy(l_tempStr, &g_other.st_pCmdOther[10], 10);
//		l_tempStr[10]='\0';
//		g_elecEnd=atoi(l_tempStr);
	}
	///delete data had success upload
    if(g_elecBegin!=0)
    {
    	sprintf(cmdStr, "DELETE FROM actpower WHERE hourTime <= %d;",g_elecBegin);
    	if(sqlite3_exec(db, cmdStr, NULL, NULL, &errmsg))
    	{
    		ERROROUT("delete success upload data failed.");
    		goto FAILED;
    	}
    	sqlite3_free(errmsg);
    	///sleep
    	myDelay(100);	///???bug???sleep 160 ms
    }
	///	get data
	sprintf(cmdStr, "SELECT hourTime,data FROM actpower;");
	if(sqlite3_get_table(db, cmdStr, &l_result, &l_row, &l_column, &errmsg))
	{
		ERROROUT("query failed.");
		goto FAILED;
	}
	///get current time
	struct timeval l_tv;
	if(-1==gettimeofday(&l_tv, NULL))
	{
		ERROROUT("get-time failed!");
		return -1;
	}
	///count begin and end time
	if(l_row ==0)
	{
		overActive_flag=0;
		g_elecBegin=g_elecEnd=0;
	}
	else if (l_row<ACTIVE_UP_NUM+1)
	{
		overActive_flag=0;
		g_elecBegin=atoi(l_result[1*l_column]);
		g_elecEnd=atoi(l_result[l_row*l_column]);
	}
	else
	{
		overActive_flag=1;
		int l_count1=0;
		int l_count2=0;
		g_elecBegin=atoi(l_result[1*l_column]);
		g_elecEnd=atoi(l_result[ACTIVE_UP_NUM*l_column]);
		for(int i=1;i<ACTIVE_UP_NUM;i++)
		{
			if (atoi(l_result[(ACTIVE_UP_NUM-i)*l_column])==g_elecEnd)
			{
				l_count1++;
			}
			else
			{
				break;
			}
			if (atoi(l_result[(ACTIVE_UP_NUM+i)*l_column])==g_elecEnd)
			{
				l_count2++;
			}
			else
			{
				break;
			}
		}
		if (l_count1<l_count2)
		{
			g_elecEnd=atoi(l_result[(ACTIVE_UP_NUM-l_count1)*l_column]);
		}
		else
			g_elecEnd=atoi(l_result[(ACTIVE_UP_NUM+l_count2)*l_column]);
	}
	sqlite3_free(errmsg);
	if(-1==devpos)		///inquiry all device's hour elec-power
	{
		for(int i=g_devList.st_equip.head;i!=-1;i=g_devList.st_equipArray[i].next)
		{
			///	typeID
			sprintf(l_tempStr, "%06x", g_devList.st_equipArray[i].typeID);
			g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
			g_other.st_pCmdOther[insertPos++]=';';
			///	socketID
			sprintf(l_tempStr,"%02x%02x%02x%02x",g_devList.st_equipArray[i].equipmentID[0],g_devList.st_equipArray[i].equipmentID[1],g_devList.st_equipArray[i].equipmentID[2],g_devList.st_equipArray[i].equipmentID[3]);
			g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
			sprintf(l_tempStr,"%02x%02x%02x%02x",g_devList.st_equipArray[i].equipmentID[4],g_devList.st_equipArray[i].equipmentID[5],g_devList.st_equipArray[i].equipmentID[6],g_devList.st_equipArray[i].equipmentID[7]);
			g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
			g_other.st_pCmdOther[insertPos++]=';';

			///	get data
			sprintf(cmdStr, "SELECT hourTime,data FROM actpower WHERE typeID = %d AND (hourTime BETWEEN %d AND %d);", g_devList.st_equipArray[i].typeID,g_elecBegin,g_elecEnd);
			if(sqlite3_get_table(db, cmdStr, &l_result, &l_row, &l_column, &errmsg))
			{
				ERROROUT("query failed.");
				goto FAILED;
			}
			///	hourtime and active-power
			for(int j=1;j<l_row+1;j++)
			{
				sprintf(l_tempStr ,"%010d", atoi(l_result[j*l_column]));
				g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[8];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[9];
				g_other.st_pCmdOther[insertPos++]=';';
				sprintf(l_tempStr, "%08f", atof(l_result[j*l_column+1]));
				g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
				g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
				g_other.st_pCmdOther[insertPos++]=';';
			}
			if (l_row<1)
			{
				g_other.st_pCmdOther[insertPos++]=';';
				g_other.st_pCmdOther[insertPos++]=';';
			}
			g_other.st_pCmdOther[insertPos++]='@';				///	seperate from devices
		}
		g_other.st_pCmdOther[insertPos++]='#';              /// add g_elecEnd value for socket
		sprintf(l_tempStr ,"%010d", g_elecEnd);
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[8];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[9];
		g_other.st_pCmdOther[insertPos++]='#';              /// add uploap over flag
		sprintf(l_tempStr ,"%02d", overActive_flag);
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
	}
	else							/// inquiry a device's hour elec-power
	{
		sprintf(l_tempStr, "%06x", g_devList.st_equipArray[devpos].typeID);
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
		g_other.st_pCmdOther[insertPos++]=';';

		///	socketID
		sprintf(l_tempStr,"%02x%02x%02x%02x",g_devList.st_equipArray[devpos].equipmentID[0],g_devList.st_equipArray[devpos].equipmentID[1],g_devList.st_equipArray[devpos].equipmentID[2],g_devList.st_equipArray[devpos].equipmentID[3]);
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
		sprintf(l_tempStr,"%02x%02x%02x%02x",g_devList.st_equipArray[devpos].equipmentID[4],g_devList.st_equipArray[devpos].equipmentID[5],g_devList.st_equipArray[devpos].equipmentID[6],g_devList.st_equipArray[devpos].equipmentID[7]);
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
		g_other.st_pCmdOther[insertPos++]=';';

		///	 get data
		sprintf(cmdStr, "SELECT hourTime,data FROM actpower WHERE typeID=%d AND (hourTime BETWEEN %d AND %d);", g_devList.st_equipArray[devpos].typeID, g_elecBegin, g_elecEnd);
		if(sqlite3_get_table(db, cmdStr, &l_result, &l_row, &l_column, &errmsg))
		{
			ERROROUT("query failed.");
			goto FAILED;
		}
		///	hourtime and elec-power
		for(int i=1;i<l_row+1;i++)
		{
			sprintf(l_tempStr, "%010d", atoi(l_result[i*l_column]));
			g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[8];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[9];
			g_other.st_pCmdOther[insertPos++]=';';
			sprintf(l_tempStr, "%08f", atof(l_result[i*l_column+1]));
			g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
			g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
			g_other.st_pCmdOther[insertPos++]=';';
		}
		if (l_row<1)
		{
			g_other.st_pCmdOther[insertPos++]=';';
			g_other.st_pCmdOther[insertPos++]=';';
		}
		g_other.st_pCmdOther[insertPos++]='@';

		g_other.st_pCmdOther[insertPos++]='#';              /// add g_elecEnd value for socket
		sprintf(l_tempStr ,"%010d", g_elecEnd);
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[2];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[3];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[4];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[5];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[6];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[7];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[8];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[9];
		g_other.st_pCmdOther[insertPos++]='#';              /// add uploap over flag
		sprintf(l_tempStr ,"%02d", overActive_flag);
		g_other.st_pCmdOther[insertPos++]=l_tempStr[0];
		g_other.st_pCmdOther[insertPos++]=l_tempStr[1];
		sqlite3_free(errmsg);
	}
	///return
	sqlite3_free_table(l_result);
	closeDatabase();
	return insertPos;

	FAILED:
	sqlite3_free_table(l_result);
	sqlite3_free(errmsg);
	closeDatabase();
	return -1;
}
/***saveActivePower() brief save active power in table of 10min and if have send station delete outdated active power
 * param devPos[in],the equipment position in g_devList.st_equipArray
 * param data[in], total power from socket
 *
 * retval int, 0 if successful or -1 failed
 ***/
int saveActivePower(int devPos, double data)
{
	char* errmsg=NULL;
	char **l_result = NULL;
	int l_row;
	int l_column;
	char l_tempStr[12];
	int l_time;

	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return -1;
	}
	struct timeval l_tv;
	if(-1==gettimeofday(&l_tv, NULL))
	{
		ERROROUT("get-time failed!");
		return -1;
	}
	l_time=l_tv.tv_sec/(YGGLGETINTERVAL)*YGGLGETINTERVAL+YGGLGETPOINT;
	///save
	sprintf(l_tempStr, "%d", g_devList.st_equipArray[devPos].typeID);
	///see if data have exist
	sprintf(cmdStr, "SELECT data FROM actpower WHERE typeID=%d AND hourTime=%d;", g_devList.st_equipArray[devPos].typeID,l_time);
	if(sqlite3_get_table(db, cmdStr, &l_result, &l_row, &l_column, &errmsg))
	{
		ERROROUT("query failed.");
		sqlite3_free_table(l_result);
		goto FAILED;
	}
	if(l_row>0)							///if data have exist, just return
	{
		PRINTOUT("%s","The data has existed.");
		goto OK;
	}
	sqlite3_free(errmsg);
	///sleep
	myDelay(50);	///???bug???sleep 50 ms
	///insert
	sprintf(cmdStr, "INSERT INTO actpower VALUES('%s',%d,%f);", l_tempStr, l_time, data);
	if(sqlite3_exec(db, cmdStr, NULL, NULL, &errmsg))
	{
		ERROROUT("save data failed.");
		goto FAILED;
	}
	sqlite3_free(errmsg);

	///return
	OK:
	sqlite3_free_table(l_result);
	sqlite3_free(errmsg);
	closeDatabase();
	return 0;

	FAILED:
	sqlite3_free(errmsg);
	closeDatabase();
	return -1;
}

/***updateDatabase() brief update database by different program version
 * param null
 *
 * retval int,0 if successful or -1 failed
 ***/
static int updateDatabase()
{
	char* errmsg=NULL;
	int l_version;					///	save the program version
	int l_verNum = 1;               /// save the second or the third version
	char** l_result = NULL;			///	save the inquiry result
	int l_row = 0;					///	save the number of row of inquiry result
	int l_column = 0;				///	save the number of column of inquiry result
	int l_typeID;					///	save the typeID temporarily

	printf("The current version is %d\n",PROVERSION);
	if(sqlite3_get_table(db,"SELECT name FROM sqlite_master WHERE type='table';",&l_result,&l_row,&l_column,&errmsg))
	{
		ERROROUT("read tables from database failed.");
		goto FAILED;
	}
	sqlite3_free(errmsg);					/// release the memory possessed by error messages
	sqlite3_free_table(l_result);				/// release the memory possessed by result

	if(l_row==0)		/// if there is no table,and just do the next that is creating tables
	{
		ERROROUT("There is no table in the database,and create tables!");
		if(-1==createTable((char*)"devlist"))			///create devlist table
			return -1;
		if(-1==createTable((char*)"power"))			///create power table
			return -1;
		if(-1==createTable((char*)"actpower"))		///create active power table
			return -1;
		if(-1==createTable((char*)"tmppower"))			///create power table
			return -1;
		if(-1==createTable((char*)"time"))			///create time table
			return -1;
		if(-1==createTable((char*)"model"))			///create model table
			return -1;
		if(-1==createTable((char*)"modtime"))			///create model-timing table
			return -1;
		if(-1==createTable((char*)"global"))			///create global table
			return -1;
#ifdef SMARTHOME_DEBUG								///use for debug, record command writed to or read from serial
		if(-1==createTable((char*)"writelog"))
			return -1;
		if(-1==createTable((char*)"readlog"))
			return -1;
#endif
	}
	else			///	if there are some tables,just check the version of program
	{
		///	inquiry the third version
		if(!(sqlite3_get_table(db,"SELECT value FROM global WHERE name='version';",&l_result,&l_row,&l_column,&errmsg)))
		{
			ERROROUT("query the third version succeed.");
			l_version=atoi(l_result[1]);
			printf("The before database version is %d\n",l_version);
			l_verNum = 3;
			sqlite3_free(errmsg);					     /// release the memory possessed by error messages
			sqlite3_free_table(l_result);				/// release the memory possessed by result
		}
		///	inquiry the second version
		else if(!(sqlite3_get_table(db,"SELECT version FROM global;",&l_result,&l_row,&l_column,&errmsg)))
		{
			ERROROUT("query the second version succeed.");
			l_version=atoi(l_result[1]);
			printf("The before database version is %d\n",l_version);
			l_verNum = 2;
			sqlite3_free(errmsg);
			sqlite3_free_table(l_result);
		}
		///inquiry before the second version
		else
		{
			ERROROUT("query other versions,no including the second and the third.");
			l_version =	20110101;
		}
		///database operation
		if((l_version == 20111122 or l_version == 20111124 or l_version == 20111231 or l_version == 20120117 or l_version == 20120220 or l_version == 20120310 \
				or l_version ==20120320) && l_verNum == 3)    ///the third version
		{
			if(l_version == PROVERSION)		///	needn't update
			{
				ERROROUT("There is no need to update database !");
			}
			else if (l_version == 20111122 or l_version == 20111124 or l_version == 20111231 or l_version == 20120117)
			{
				ERROROUT("update the third database including actpower,tmppower and version!");
				if(-1==createTable((char*)"actpower"))
					return -1;
				if(-1==createTable((char*)"tmppower"))
					return -1;
				sprintf(cmdStr,"UPDATE global SET value=%d WHERE name='version';",PROVERSION);
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("insert 'global' failed.");
					goto FAILED;
				}
				sqlite3_free(errmsg);
			}
			else							///	need update
			{
				ERROROUT("update the third database including version!");
				sprintf(cmdStr,"UPDATE global SET value=%d WHERE name='version';",PROVERSION);
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("insert 'global' failed.");
					goto FAILED;
				}
				sqlite3_free(errmsg);
			}
		}
		else if((l_version == 20110602 or l_version == 20110824) && l_verNum == 2)    ///the second version
		{
			if(l_version == PROVERSION)		///	needn't update
			{
				ERROROUT("There is no need to update database !");
			}
			else							///	need update
			{
				ERROROUT("update the second database including version!");
				if(sqlite3_get_table(db,"SELECT name FROM sqlite_master WHERE type='table';",&l_result,&l_row,&l_column,&errmsg))
				{
					ERROROUT("read tables from database failed.");
					goto FAILED;
				}

				sprintf(cmdStr,"Drop table day;");						///	delte the table day
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("delete table day failure.");
					goto FAILED;
				}

				sprintf(cmdStr,"Drop table total;");						///	delete the table total
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("delete table total failure.");
					goto FAILED;
				}

				///	create table power
				sprintf(cmdStr,"CREATE TABLE power(typeID INTEGER,hourTime INTEGER,data DOUBLE,FOREIGN KEY(typeID) REFERENCES devlist(typeID) ON UPDATE CASCADE ON DELETE CASCADE);");
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("create 'power' failed.");
					goto FAILED;
				}

				sprintf(cmdStr,"SELECT * FROM devlist;");					///	delete tables typeID
				if(sqlite3_get_table(db,cmdStr,&l_result,&l_row,&l_column,&errmsg))
				{
					ERROROUT("query devlist failed.");
					goto FAILED;
				}
				for(int i=2;i<(l_row+1);i++)
				{
					l_typeID=atoi(l_result[i*l_column]);
					sprintf(cmdStr,"INSERT INTO power(typeID,hourTime,data) SELECT '%d',hourTime,data FROM '%d';",l_typeID,l_typeID);
					if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
					{
						ERROROUT("Insert data into table power failure.");
						goto FAILED;
					}
					sprintf(cmdStr,"Drop table '%d';",l_typeID);
					if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
					{
						ERROROUT("delete table typeID failure.");
						goto FAILED;
					}
				}

				///	create table modtime
				sprintf(cmdStr,"CREATE TABLE modtime(typeID INTEGER,cmdType INTEGER,controlVal INTEGER,controlTime INTEGER,frequency INTEGER,remark NVARCHAR(40),modeID INTEGER,FOREIGN KEY(typeID) REFERENCES devlist(typeID) ON UPDATE CASCADE ON DELETE CASCADE);");
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("create 'modtime' failed.");
					goto FAILED;
				}

				///	modify table time(delete the modID column)
				///	copy model-time to table modtime
				sprintf(cmdStr,"INSERT INTO modtime SELECT typeID,cmdType,controlVal,controlTime,frequency,remark,modeID FROM time WHERE modeID!=0;");
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("insert data into 'modtime' failed.");
					goto FAILED;
				}
				///	change the table time into temptime
				sprintf(cmdStr,"ALTER table time RENAME TO temptime;");
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("modify table name failure.");
					goto FAILED;
				}
				///	create a new table time
				sprintf(cmdStr,"CREATE TABLE time(typeID INTEGER,cmdType INTEGER,controlVal INTEGER,controlTime INTEGER,frequency INTEGER,remark NVARCHAR(40),FOREIGN KEY(typeID) REFERENCES devlist(typeID) ON UPDATE CASCADE ON DELETE CASCADE);");
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("create 'time' failed.");
					goto FAILED;
				}
				///	copy data of temptime to the new time
				sprintf(cmdStr,"INSERT INTO time SELECT typeID,cmdType,controlVal,controlTime,frequency,remark FROM temptime WHERE modeID=0;");
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("insert data into 'time' failed.");
					goto FAILED;
				}
				sprintf(cmdStr,"Drop table temptime;");						///	delete the table temptime
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("delete table temptime failure.");
					goto FAILED;
				}
				///	modify table global
				sprintf(cmdStr,"Drop table global;");						///	delete the table global
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("delete table global failure.");
					goto FAILED;
				}
				///	create a new table global
				sprintf(cmdStr,"CREATE TABLE global(ID INTEGER PRIMARY KEY AUTOINCREMENT,name NVARCHAR(20),value NVARCHAR(20));");
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("create 'global' failed.");
					goto FAILED;
				}
				sqlite3_free(errmsg);
				///version
				sprintf(cmdStr,"INSERT INTO global VALUES(NULL,'version','%d');",PROVERSION);
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("insert 'global' failed.");
					goto FAILED;
				}
				sqlite3_free(errmsg);
				///username
				sprintf(cmdStr,"INSERT INTO global VALUES(NULL,'username','admin');");
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("insert 'global' failed.");
					goto FAILED;
				}
				sqlite3_free(errmsg);
				///passwd
				sprintf(cmdStr,"INSERT INTO global VALUES(NULL,'passwd','admin');");
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("insert 'global' failed.");
					goto FAILED;
				}
				sqlite3_free(errmsg);
				///serialNUM
				sprintf(cmdStr,"INSERT INTO global VALUES(NULL,'serialNUM','');");
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("insert 'global' failed.");
					goto FAILED;
				}
				sqlite3_free(errmsg);
				///serverIP
				sprintf(cmdStr,"INSERT INTO global VALUES(NULL,'serverIP','');");
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("insert 'global' failed.");
					goto FAILED;
				}
				sqlite3_free(errmsg);
				///port
				sprintf(cmdStr,"INSERT INTO global VALUES(NULL,'port','');");
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("insert 'global' failed.");
					goto FAILED;
				}
				sqlite3_free(errmsg);
				sqlite3_free_table(l_result);
                ///update 2 to 3
				if(-1==createTable((char*)"actpower"))
					return -1;
				if(-1==createTable((char*)"tmppower"))
					return -1;
			}
		}
		else                     ///before the second version
		{
			if(sqlite3_get_table(db,"SELECT name FROM sqlite_master WHERE type='table';",&l_result,&l_row,&l_column,&errmsg))
			{
				ERROROUT("read tables from database failed.");
				goto FAILED;
			}
			///delete all tables
			for(int l_i=0;l_i<l_row;l_i++)								///l_column=1;the first row is result[0]=name;result[1]=sqlite_sequence;and the other is the table names one in every row.
			{
				if(!strcmp(l_result[l_i+1],(char*)"sqlite_sequence"))
					continue;
				sprintf(cmdStr,"Drop table '%s';",l_result[l_i+1]);
				if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
				{
					ERROROUT("delete table  failure.");
					goto FAILED;
				}
			}
			sqlite3_free(errmsg);
			sqlite3_free_table(l_result);
			ERROROUT("There is no table in the database,and create tables!");
			if(-1==createTable((char*)"devlist"))			///create devlist table
				return -1;
			if(-1==createTable((char*)"power"))			///create power table
				return -1;
			if(-1==createTable((char*)"actpower"))		///create active power table
				return -1;
			if(-1==createTable((char*)"tmppower"))			///create power table
				return -1;
			if(-1==createTable((char*)"time"))			///create time table
				return -1;
			if(-1==createTable((char*)"model"))			///create model table
				return -1;
			if(-1==createTable((char*)"modtime"))			///create model-timing table
				return -1;
			if(-1==createTable((char*)"global"))			///create global table
				return -1;
#ifdef SMARTHOME_DEBUG								///use for debug, record command writed to or read from serial
			if(-1==createTable((char*)"writelog"))
				return -1;
			if(-1==createTable((char*)"readlog"))
				return -1;
#endif
		}
	}
	//close database
	closeDatabase();
	return 0;						/// quit

	FAILED:
	closeDatabase();
	return -1;						/// quit
}
/***delDataByChTime() brief delete overdue data from database while changing system's time
 * param timeStamp[in], new timeStamp of system
 *
 * retval void
 ***/
void delDataByChTime(int timeStamp)
{
	char* errmsg=NULL;
	int l_typeID=-1;
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return;
	}
	if(0>=timeStamp)						///modeID must greater than 0
		goto FAILED;

	///delete data in 'power'
	sem_wait(&g_devList.st_equip.st_sem);///!!!!!
	for(int i=g_devList.st_equip.head;i!=-1;i=g_devList.st_equipArray[i].next)
	{
		l_typeID=g_devList.st_equipArray[i].typeID;
		///delete data in 'power'
		sprintf(cmdStr,"DELETE FROM power WHERE typeID=%d AND hourTime >= %d;",l_typeID,timeStamp);
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("update data failed.");
			sem_post(&g_devList.st_equip.st_sem);///!!!!!
			goto FAILED;
		}
		///delete data in 'actpower'
		sprintf(cmdStr,"DELETE FROM actpower WHERE typeID=%d AND hourTime >= %d;",l_typeID,timeStamp);
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("update data failed.");
			sem_post(&g_devList.st_equip.st_sem);///!!!!!
			goto FAILED;
		}
		///delete data in 'tmppower'
		sprintf(cmdStr,"DELETE FROM tmppower WHERE typeID=%d AND hourTime >= %d;",l_typeID,timeStamp);
		if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
		{
			ERROROUT("update data failed.");
			sem_post(&g_devList.st_equip.st_sem);///!!!!!
			goto FAILED;
		}
		l_typeID=-1;
		sqlite3_free(errmsg);
	}
	sem_post(&g_devList.st_equip.st_sem);///!!!!!
	///return
	closeDatabase();
	return;

	FAILED:
	sqlite3_free(errmsg);
	closeDatabase();
	return;
}

/***getTodayZero() brief get today zero value
 * param todaytime[in], the day time value
 *
 * retval int,the day's zero value
 ***/
int getTodayZero(int todaytime)
{
	int l_tmpZero;
	l_tmpZero=todaytime%(3600*24);
	if(l_tmpZero<57600)
	{
		l_tmpZero=(todaytime/(3600*24)-1)*3600*24+57600;
	}
	else
	{
		l_tmpZero=todaytime/(3600*24)*3600*24+57600;
	}
	return l_tmpZero;
}
void delOutdatedData()
{
	char* errmsgDel=NULL;
	int l_tmpTime;
	int l_typeID = -1;
	///open database
	if(-1==openDatabase())
	{
		ERROROUT("Open database failed.");
		return;
	}
	struct timeval l_tv;
	if(-1==gettimeofday(&l_tv, NULL))
	{
		ERROROUT("get-time failed!");
		return;
	}
	///delete outdated elec-power and the longest time is 6 months
	///adjust time stamp
	l_tmpTime=getTodayZero(l_tv.tv_sec);
	l_tmpTime = l_tmpTime-STORAGETIME;											///	delete outdated elec-power in every 00:00
	sem_wait(&g_devList.st_equip.st_sem);
	for(int i=g_devList.st_equip.head;i!=-1;i=g_devList.st_equipArray[i].next)					/// delete every device's outdated elec-power
	{
		l_typeID = g_devList.st_equipArray[i].typeID;
		sprintf(cmdStr, "DELETE FROM power WHERE typeID = %d AND hourTime <= %d;", l_typeID, l_tmpTime);
		if(sqlite3_exec(db, cmdStr, NULL, NULL, &errmsgDel))
		{
			ERROROUT("delete outdate data failed.");
			sem_post(&g_devList.st_equip.st_sem);
			goto FAILED;
		}
		l_typeID=-1;
		sqlite3_free(errmsgDel);
	}
	sem_post(&g_devList.st_equip.st_sem);
	///return
	closeDatabase();
	return;

	FAILED:
	sqlite3_free(errmsgDel);
	closeDatabase();
	return;

}
/***saveComInDatabase() brief save command to database.
 *	param 	buf[in], data to be saved
 *	param   len[in], data length
 *	param   flag[in],1 present data to be writed to serail or 0 present read from serial.
 *
 *	retval  null.
 ***/
#ifdef SMARTHOME_DEBUG
void saveComInDatabase(unsigned char* buf,int len,bool flag)
{
//	char* errmsg=NULL;
//	char l_time[12];
//	char l_comStr[48];
//	struct timeval l_tv;
//	struct tm *l_tm=NULL;
//
//	if(-1==openDatabase())
//		return;
//
//	if(gettimeofday(&l_tv,NULL)==-1)							///get the currunt time as timestamp
//	{
//		PRINTOUT("%s","gettimeofday() failed!");
//		return;
//	}
//	l_tm=localtime(&(l_tv.tv_sec));
//	sprintf(l_time,"%02d%02d%02d%02d%02d",l_tm->tm_mon+1,l_tm->tm_mday,l_tm->tm_hour,l_tm->tm_min,l_tm->tm_sec);
//
//	for(int i=0;i<len;i++)
//	{
//		sprintf(&l_comStr[i*2],"%02x",buf[i]);
//	}
//
//	if(flag)
//		sprintf(cmdStr,"INSERT INTO writelog VALUES(NULL,'%s','%s');",l_comStr,l_time);
//	else
//		sprintf(cmdStr,"INSERT INTO readlog VALUES(NULL,'%s','%s');",l_comStr,l_time);
//
//	if(sqlite3_exec(db,cmdStr,NULL,NULL,&errmsg))
//	{
//		ERROROUT("insert failed.");
//		sqlite3_free(errmsg);
//		closeDatabase();
//		return;
//	}
//	sqlite3_free(errmsg);
//	closeDatabase();
}
#endif
